<?php

namespace app\purchase\model;

use think\Db;

class Client extends \ShopModel
{
    protected $table = 'sh_client';

    protected $pk = 'customer_id';

    protected $autoWriteTimestamp = true;
    protected $updateTime = false;
    protected $createTime = 'create_at';

//    public function user()
//    {
//        return $this->hasOne('User', 'user_id', 'user_id');
//    }
//
//    public function caccount(){
//        return $this->hasOne('CustomerAccount','customer_id');
//    }
//
//    public function orders(){
//        return $this->hasMany('Order','order_id');
//    }
//
//    public function cpersonal(){
//        return $this->hasOne('CustomerPersonal','customer_id');
//    }
//
//    public function cfinance(){
//        return $this->hasOne('CustomerFinance','customer_id');
//    }
    public function level()
    {
        return $this->hasOne('ClientLevel', 'level_id','level_id');
    }
    public function department()
    {
        return $this->hasOne('Department', 'department_id','department_id');
    }
    public function salesman(){
        return $this->hasOne('Salesman', 'salesman_id','salesman_id');
    }
    public function paymentType(){
        return $this->hasOne('PaymentType', 'pdt_id','pdt_id');
    }
    public function warehouse(){
    return $this->hasOne('Warehouse', 'warehouse_id','warehouse_id');
}
    public function area(){
        return $this->hasOne('Area', 'area_id','area_id');
    }
    public function clientbill(){
        return $this->hasOne('Clientbill', 'client_id','client_id');
    }
    public function ClientDb(){
        return Db::name('client');
    }
    /**
     * 搜索
     */
    public function search($params){
        $page = !empty($params['page']) ? $params['page'] : 1;
        $pageSize = !empty($params['pageSize']) ? $params['pageSize'] : 20;
        $where = [];

        if(!empty($params['level_id'])){
            $where['client.level_id'] = $params['level_id'];
        }
        if(!empty($params['salesman_id'])){
            $where['client.salesman_id'] = $params['salesman_id'];
        }

        if(!empty($params['pdt_id'])){
            $where['client.pdt_id'] = $params['pdt_id'];
        }

        if(!empty($params['area'])){
            $Area =  new Area();
            $params['area'] = $Area->searchSrt($params['area']);
            $where['client.area'] = ['like','%'.$params['area'].'%'];
        }

        if(!empty($params['is_dh'])){
            $where['client.is_dh'] = $params['is_dh'];
        }

        if(!empty($params['client_source'])){
            $where['account.client_source'] = $params['client_source'];
        }
        if(!empty($params['department_id'])){
            $where['client.department_id'] = $params['department_id'];
        }
        if(!empty($params['date'][0])&&!empty($params['date'][1])){
            $where['sign_start'] = ['<=',strtotime($params['date'][0])];
            $where['sign_end'] = ['>=',strtotime($params['date'][1])];
        }
        if(!empty($params['query'])){
            $where['client.client_name | client.client_sn | client.is_dh | client.name | client.mobile'] = ['like','%'.$params['query'].'%'];
        }

        $data = $this->alias('client')
            ->join('sh_client_level level','client.level_id = level.level_id','left')
            ->join('sh_salesman salesman','client.salesman_id = salesman.salesman_id','left')
            ->join('sh_department department','client.department_id = department.department_id','left')
            ->field('client.client_id,client.client_name, client.client_sn, client.user_name, client.area, level.level_name, client.name, client.mobile, client.client_add_time, client.is_dh, salesman.salesman_name, client.client_source, department.department_name')
            ->where($where)
            ->where(  'client.is_delete' ,0)
            ->page($page,$pageSize)
            ->order('client.client_id desc')
            ->select();

        foreach($data as &$v){
            $v['is_dh'] = $this->getStatusTrans($v['is_dh']);
            $v['area'] = explode(',',$v['area']);
            $v['client_add_time'] = date('Y-m-d',$v['client_add_time']);
            $v['client_source'] = $this->getSourceTrans($v['client_source']);
        }

        //获取分页数据
        $count = $this->alias('client')
            ->join('sh_client_level level','client.level_id = level.level_id','left')
            ->join('sh_salesman salesman','client.salesman_id = salesman.salesman_id','left')
            ->join('sh_department department','client.department_id = department.department_id','left')
            ->field('client.client_id')
            ->where($where)
            ->where(  'client.is_delete' ,0)
            ->count();

        $pager = [
            'totalNum' => $count,
            'currentPage' => $page,
            'pageSize' => $pageSize
        ];

        

        return [
            'client' => $data,
            'pager' => $pager
        ];

    }

    /**
     * 生成密码盐
     */
    public function generateSalt()
    {
        $str = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_';
        $strMaxIndex = strlen($str) - 1;
        $salt = '';
        $saltLenght = 6;
        for ($i = 0; $i < $saltLenght; $i ++) {
            $salt .= $str[mt_rand(0, $strMaxIndex)];
        }
        return $salt;
    }
//    /**
//     * 高级搜索
//     */
//    public function seniorSearch($params){
//
//        $page = !empty($params['page']) ? $params['page'] : 1;
//        $pageSize = !empty($params['pageSize']) ? $params['pageSize'] : 20;
//
//        $where = [];
//
//        if(!empty($params['level_id'])){
//            $where['client.level_id'] = $params['level_id'];
//        }
//
//        if(!empty($params['query'])){
//            $where['client.client_name | client.client_sn | client.is_dh | personal.name | personal.mobile'] = ['like','%'.$params['query'].'%'];
//        }
//
//        if(!empty($params['salesman_id'])){
//            $where['client.salesman_id'] = $params['salesman_id'];
//        }
//
//        if(!empty($params['pdt_id'])){
//            $where['client.pdt_id'] = $params['pdt_id'];
//        }
//
//        if(!empty($params['area_id'])){
//            $where['client.area_id'] = $params['area_id'];
//        }
//
//        if(!empty($params['is_dh'])){
//            $where['customer.is_dh'] = $params['is_dh'];
//        }
//
//        if(!empty($params['client_source'])){
//            $where['account.client_source'] = $params['client_source'];
//        }
//        if(!empty($params['start_time'])&&!empty($params['end_time'])){
//            $where['client_add_time'] = ['<=',$params['start_time']];
//            $where['client_add_time'] = ['>=',$params['end_time']];
//        }
//        $data = $this->alias('client')
//            ->join('sh_client_personal personal','client.client_id = personal.client_id','left')
//            ->join('sh_client_level level','client.level_id = level.level_id','left')
//            ->join('sh_fax fax','client.area_id = fax.fax_id','left')
//            ->join('sh_salesman salesman','client.salesman_id = salesman.salesman_id','left')
//            ->join('sh_department department','client.department_id = department.department_id','left')
//            ->field('client.client_name, client.client_sn, client.user_name, fax.fax_name, level.level_name, personal.name, personal.mobile, client.client_add_time, client.is_dh, salesman.salesman_name, client.client_source, department.department_name')
//            ->where($where)
//            ->page($page,$pageSize)
//            ->order('client.client_id desc')
//            ->select();
//
////        $time = time();
////
////        $where['start_time'] = ['<=',$time];
////
////        $where['end_time'] = ['>=',$time];
//
//
//
//        foreach($data as &$v){
//            $v['is_dh'] = $this->getStatusTrans($v['is_dh']);
//            $v['client_source'] = $this->getSourceTrans($v['client_source']);
//        }
//
//        return $data;
//
//    }

    public function getStatusTrans($status){
        $all_status = array(
            0 => '未开通',
            1 => '已开通',
            2 => '禁用'
        );
        return !empty($all_status[$status]) ? $all_status[$status] : '未知';
    }
    public function getSourceTrans($source){
        $all_status = array(
            0 => '自主注册',
            1 => '手工创建',
            2 => '批量导入'
        );
        return !empty($all_status[$source]) ? $all_status[$source] : '未知';
    }

}
